package com.yzz.hzraisedust.hzraseidust.main;

import android.content.Context;
import android.content.SharedPreferences;
import android.widget.Toast;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.google.gson.Gson;
import com.yzz.hzraisedust.hzraseidust.modelbean.MapInfoBean;
import com.yzz.hzraisedust.hzraseidust.modelbean.MapItemBean;
import com.yzz.hzraisedust.hzraseidust.util.Constant;
import com.yzz.hzraisedust.hzraseidust.util.NetUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by zhengzhen on 2017/8/19.
 */

public class MapInfoControl implements Response.Listener<String>,Response.ErrorListener{
    private MapInfoBean mMapInfoBean;
    private MapInfoBean mShowMapInfoBean;
    private Context mContext;
    private  Response.Listener<MapInfoBean> mListener;
    private List<String> mAreaConditions;
    private List<String> mAirQualityConditions;
    public void setContext(Context mContext) {
        this.mContext = mContext;
    }

    public void setListener(Response.Listener<MapInfoBean> mListener) {
        this.mListener = mListener;
    }

    public List<String> getmAirQualityConditions() {
        return mAirQualityConditions;
    }

    public List<String> getmAreaConditions() {
        return mAreaConditions;
    }

    public MapInfoBean getMapInfoBean() {
        return mMapInfoBean;
    }

    public MapInfoBean getmShowMapInfoBean() {
        return mShowMapInfoBean;
    }

    public MapInfoControl(Context context){
        mContext = context;
        mAreaConditions = new ArrayList<>();
        mAirQualityConditions = new ArrayList<>();
    }

    /**
     * 请求网络数据
     */
    public void requstData(){
        NetUtil netUtil = new NetUtil(mContext);
        //获取Uid
        SharedPreferences sharedPreferences = mContext.getSharedPreferences(Constant.USERINFO,Context.MODE_PRIVATE);
        String uid = sharedPreferences.getString(Constant.UID_KEY,"");
        Map<String,String> params = new HashMap<>();
        params.put("uid",uid);
        netUtil.requestData(this,this, Constant.MAPINFO_URL,params);
    }

    @Override
    public void onResponse(String s) {
        Gson gson = new Gson();
        mMapInfoBean = gson.fromJson(s,MapInfoBean.class);
        if(mMapInfoBean.isSuccess()) {
            setShowInfo();
            this.mListener.onResponse(mMapInfoBean);
        }else{
            Toast.makeText(mContext,"加载失败"+mMapInfoBean.getMessage(),Toast.LENGTH_LONG).show();
            ((BaseActivity)mContext).closeLoginingDlg();
        }

    }

    private void setShowInfo() {
        mAreaConditions.clear();
        mAirQualityConditions.clear();
        mAreaConditions.add("全部");
        mAirQualityConditions.add("全部");
        mShowMapInfoBean = new MapInfoBean();
        mShowMapInfoBean.setRows(new ArrayList<MapItemBean>());
        for (MapItemBean item: mMapInfoBean.getRows()) {
            mShowMapInfoBean.getRows().add(item);
            //城区
            String areaName =item.getCityName();
            if(areaName!=null) {
                if (!mAreaConditions.contains(areaName)) {
                    mAreaConditions.add(areaName);
                }
            }
            String describe = item.getPmDescribe();
            if(describe == null ||describe == ""){
                item.concute();
                describe = item.getPmDescribe();
            }
            if(!mAirQualityConditions.contains(describe)){
                mAirQualityConditions.add(describe);
            }

        }
    }

    @Override
    public void onErrorResponse(VolleyError volleyError) {
        Toast.makeText(mContext,"加载失败"+volleyError.getLocalizedMessage(),Toast.LENGTH_LONG).show();
        ((BaseActivity)mContext).closeLoginingDlg();
    }

    public void searAreaAndQualityItems(String areaName,String airContidion){
        mShowMapInfoBean.getRows().clear();
        for(MapItemBean item :mMapInfoBean.getRows()){
            if((areaName.equals("全部")|| areaName.equals(item.getCityName())) && (airContidion.equals("全部")||airContidion.equals(item.getPmDescribe()))){
                mShowMapInfoBean.getRows().add(item);
            }
        }
    }

}
